package com.haoyeteng.sxbd.modular.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.constant.DeleteFlag;
import com.haoyeteng.sxbd.core.constant.SxbdConstants;
import com.haoyeteng.sxbd.core.exception.SxbdException;
import com.haoyeteng.sxbd.core.util.ToolUtil;
import com.haoyeteng.sxbd.modular.business.model.Coordinate;
import com.haoyeteng.sxbd.modular.business.model.EleFence;
import com.haoyeteng.sxbd.modular.business.dao.EleFenceMapper;
import com.haoyeteng.sxbd.modular.business.service.ICoordinateService;
import com.haoyeteng.sxbd.modular.business.service.IEleFenceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电子围栏表 服务实现类
 * </p>
 *
 * @author haoyeteng123
 * @since 2019-07-06
 */
@Service
public class EleFenceServiceImpl extends ServiceImpl<EleFenceMapper, EleFence> implements IEleFenceService {

    @Autowired
    private ICoordinateService coordinateService;

    /**
     * 围栏分页查询
     * @param page 分页
     * @param params 参数
     * @return List<map>
     */
    @Override
    public List<Map<String, Object>> selectEleFences(Page<EleFence> page, Map<String,Object> params){
        return this.baseMapper.selectEleFences(page, params);
    }

    /**
     * 插入围栏和明细坐标点
     * @param eleFence 围栏
     */
    @Override
    public void insertAndDtl(EleFence eleFence){
        List<Coordinate> details = eleFence.getDetails();
        if (ToolUtil.isEmpty(details)) {
            throw new SxbdException("坐标点集合为空！");
        }
        Date date = new Date();
        eleFence.setCreateType(SxbdConstants.ELEFENCE_CREATE_TYPE_MAP);
        eleFence.setCreateTime(date);
        eleFence.setUpdateTime(date);
        eleFence.setFlag(DeleteFlag.OK.getCode());
        this.baseMapper.insert(eleFence);

        for (Coordinate coordinate : details) {
            coordinate.setEleFenceId(eleFence.getEleFenceId());
            coordinateService.insert(coordinate);
        }
    }

    /**
     * 获取围栏及明细坐标点
     * @param eleFenceId 围栏Id
     */
    @Override
    public EleFence selectAndDtlById(Integer eleFenceId){
        EleFence eleFence = this.baseMapper.selectById(eleFenceId);
        if (ToolUtil.isNotEmpty(eleFence)) {
            List<Coordinate> coordinates = coordinateService.selectList(new EntityWrapper<Coordinate>().eq("ele_fence_id", eleFenceId).orderBy("point_no"));
            eleFence.setDetails(coordinates);
        }
        return eleFence;
    }

    /**
     * 更新围栏
     * 1.更新围栏
     * 2.删除明细坐标点
     * 3.插入新明细坐标点
     * @param eleFence 围栏
     */
    @Override
    public void updateAndDtl(EleFence eleFence){
        eleFence.setUpdateTime(new Date());
        //1.更新围栏
        this.baseMapper.updateById(eleFence);
        //2.删除明细坐标点
        if (ToolUtil.isNotEmpty(eleFence.getDetails())) {
            coordinateService.delete(new EntityWrapper<Coordinate>().eq("ele_fence_id", eleFence.getEleFenceId()));
            //3.插入新明细坐标点
            for (Coordinate coordinate : eleFence.getDetails()) {
                coordinateService.insert(coordinate);
            }
        }
    }

}
